<?php
/**
 * @author wyl
* @date 2015-8-10 15:51:23
*/
Yii::import('system.web.widgets.CWidget');
class CImageCarousel extends CWidget{
	public $id;	
	public $images=array();
	
	public function init(){
		if(empty($this->id)){
			$this->id='carousel-image-'.time();
		}	
	}
	
	public function run(){
		echo CHtml::openTag('div',array(
									'id'		=>$this->id,
									'class'		=>'carousel slide',
									'data-ride'	=>'carousel'
							));
		$this->renderIndicator();
		$this->renderBody();
		$this->renderControl();
		echo CHtml::closeTag('div');
	}
	
	public function renderIndicator(){
		if(count($this->images)>0){
			$i=0;
			echo CHtml::openTag('ol',array('class'=>'carousel-indicators'));
				foreach($this->images as $key=>$img){
					echo CHtml::openTag('li',array(
							'data-target'	=>'#'.$this->id,
							'data-slide-to'	=>$i,
							'class'			=>(!empty($img['active']))?'active':''
					));
					echo CHtml::closeTag('li');
					$i++;
				}
			echo CHtml::closeTag('ol');
		}
	}
	
	public function renderBody(){
		if(count($this->images)>0){
			echo CHtml::openTag('div',array(
				'class'		=>'carousel-inner',
				'role'		=>'listbox'
			));
			$i=0;
			foreach($this->images as $key=>$img){
				echo CHtml::openTag('div',array(
						'class'=>(!empty($img['active']))?'active item':''.' item'
				));
					echo CHtml::image($img['src']);
					echo CHtml::openTag('div',array(
						'class'=>'carousel-caption'			
					));
						echo $img['caption'];
					echo CHtml::closeTag('div');
				echo CHtml::closeTag('div');
				$i++;
			}
			echo CHtml::closeTag('div');
		}
	}
	
	public function renderControl(){
		echo CHtml::openTag('a',array(
				'class'		=>'left carousel-control',
				'href'		=>'#'.$this->id,
				'role'		=>'button',
				'data-slide'=>'prev'
		));
			echo CHtml::tag('span',array(
					'arial-hidden' 	=>'true',
					'class'			=>'glyphicon glyphicon-chevron-left'	
			));
		echo CHtml::closeTag('a');
		
		echo CHtml::openTag('a',array(
				'class'		=>'right carousel-control',
				'href'		=>'#'.$this->id,
				'role'		=>'button',
				'data-slide'=>'next'
		));
			echo CHtml::tag('span',array(
					'arial-hidden' 	=>'true',
					'class'			=>'glyphicon glyphicon-chevron-right'
			));
		echo CHtml::closeTag('a');
	}
	
}		
